function loadCar(){
	var carData = JSON.parse(getCar());

	if(carData){
		var tbody = document.getElementsByTagName("tbody")[0];
		for (var i = 0; i < carData.length; i++) {
			var tr = document.createElement("tr");
			tr.setAttribute("id",carData[i].id);
			var td1 = document.createElement("td");	
			var inputCheck = document.createElement("input");
			inputCheck.setAttribute("type","checkbox");
			inputCheck.setAttribute("class","cbox");
			inputCheck.setAttribute("checked", "checked");
			inputCheck.setAttribute("onchange","checkChange()")
			td1.appendChild(inputCheck);
			tr.appendChild(td1);
			
			var td2 = document.createElement("td");
			var a2 = document.createElement("a");
			a2.setAttribute("href","pro_details.html");
			var img2 = document.createElement("img");
			img2.src = carData[i].imgSrc;
			var h4 = document.createElement("h4");
			var a2_1 = document.createElement("a");
			a2_1.setAttribute("href","pro_details.html");
			var text2_1 = document.createTextNode("电炖锅煲汤锅白瓷陶瓷隔水全自动炖汤煮粥锅电炖盅");
			var span2 = document.createElement("span");
			span2.setAttribute("class","color_sort");
			var text2_2 = document.createTextNode("颜色分类 灰色");
			a2_1.appendChild(text2_1)
			h4.appendChild(a2_1);
			span2.appendChild(text2_2);
			a2.appendChild(img2);
			td2.appendChild(a2);
			td2.appendChild(h4);
			td2.appendChild(span2);
			tr.appendChild(td2);
			
			var td3 = document.createElement("td");
			var span3_1 = document.createElement("span");
			span3_1.setAttribute("class","reduce");
			span3_1.setAttribute("onclick","reduceNum(this)");
			var text3_1 = document.createTextNode("-");
			var inputText = document.createElement("input");
			inputText.setAttribute("type","text");
			inputText.setAttribute("value",carData[i].num);
			inputText.setAttribute("class","num");
			inputText.setAttribute("onblur","numChange(this)");
			var span3_2 = document.createElement("span");
			span3_2.setAttribute("class","add");
			span3_2.setAttribute("onclick","addNum(this)");
			var text3_2 = document.createTextNode("+");
			span3_1.appendChild(text3_1);
			span3_2.appendChild(text3_2);
			td3.appendChild(span3_1);
			td3.appendChild(inputText);
			td3.appendChild(span3_2);
			tr.appendChild(td3)
			
			var td4 = document.createElement("td");
			var span4 = document.createElement("span");
			span4.setAttribute("class","unit");
			var text4 = document.createTextNode("199.90");
			span4.appendChild(text4);
			td4.appendChild(span4);
			tr.appendChild(td4);
			
			var td5 = document.createElement("td");
			var span5 = document.createElement("span");
			span5.setAttribute("class","total_price");
			var text5 = document.createTextNode(carData[i].totalPrice);
			span5.appendChild(text5);
			td5.appendChild(span5);
			tr.appendChild(td5);
			
			var td6 = document.createElement("td");
			var span6_1 = document.createElement("span");
			var a6_1 = document.createElement("a");
			a6_1.setAttribute("href","javascript:void(0)");
			var text6_1 = document.createTextNode("移入收藏夹");
			var br = document.createElement("br");
			var span6_2 = document.createElement("span");
			span6_2.setAttribute("class","del");
			var a6_2 = document.createElement("a");
			a6_2.setAttribute("href","javascript:void(0)");
			a6_2.setAttribute("onclick","del(this)");
			var text6_2 = document.createTextNode("删除");
			a6_1.appendChild(text6_1);
			span6_1.appendChild(a6_1);
			a6_2.appendChild(text6_2);
			span6_2.appendChild(a6_2);
			td6.appendChild(span6_1);
			td6.appendChild(br);
			td6.appendChild(span6_2);
			tr.appendChild(td6);
			
			tbody.appendChild(tr);
		}
	}
}


var checkAll = document.getElementsByClassName("checkAll");
var cbox = document.getElementsByClassName("cbox");
var reduce = document.getElementsByClassName("reduce");
var add = document.getElementsByClassName("add");
var num = document.getElementsByClassName("num");
var total = document.getElementsByClassName("total_price");
var unit = document.getElementsByClassName("unit");
var choose_num = document.getElementById("choose_num");
var sum = document.getElementById("sum");
var price = document.getElementsByClassName("price")[0];
var index = 0;


for(var i = 0; i < checkAll.length; i++ ){
	checkAll[i].onclick = function(){
	if(this.checked){
		checkAll[0].checked = 1;
		checkAll[1].checked = 1;
		for (var j = 0; j<cbox.length;j++) {
			cbox[j].checked = 1;
		}
		choose_num.innerText = cbox.length;
	}else{
		checkAll[0].checked = 0;
		checkAll[1].checked = 0;
		for (var j = 0; j<cbox.length;j++) {
			cbox[j].checked = 0;
			}
		choose_num.innerHTML = 0;
		}
	totalMoney();	
	}
}

function checkChange(){
	if (!this.checked) {
		    checkAll[0].checked = 0;
			checkAll[1].checked = 0;
		}
		index = 0;
			for (var j = 0;j<cbox.length;j++) {
		         if (cbox[j].checked==1){
		         	index++;
		         	if (index==cbox.length){
		         		checkAll[0].checked = 1;
						checkAll[1].checked = 1;
		         	}
		         }
		         choose_num.innerText = index;
		         
			}
		totalMoney();	
}

function addNum(obj){
	var id = obj.parentNode.parentNode.getAttribute("id");
	var pro_num = parseInt(obj.previousSibling.value);
	pro_num += 1;
	obj.previousSibling.value = pro_num;
	var unit = obj.parentNode.nextSibling.childNodes[0];
	var total = obj.parentNode.nextSibling.nextSibling.childNodes[0];	
	total.innerText = (pro_num * parseFloat(unit.innerText)).toFixed(2);
	totalMoney();
	changeCarNum(id,pro_num);
}

function reduceNum(obj){
	var id = obj.parentNode.parentNode.getAttribute("id");
	var pro_num = parseInt(obj.nextSibling.value);
	pro_num -= 1;
	if(obj.nextSibling.value <= 1){
		pro_num = 1;
	}
	obj.nextSibling.value = pro_num;
	var unit = obj.parentNode.nextSibling.childNodes[0];
	var total = obj.parentNode.nextSibling.nextSibling.childNodes[0];
	total.innerText = (pro_num * parseFloat(unit.innerText)).toFixed(2);
	totalMoney();
	changeCarNum(id,pro_num);
}

function numChange(obj){
	var id = obj.parentNode.parentNode.getAttribute("id");
	var num = obj.value;
	if(isNaN(num)){
		num = 1;
	} else if (num < 1) {
		num = 1;
	}
	num = parseInt(num);
	obj.value = num;
	var unit = obj.parentNode.nextSibling.childNodes[0];
	var total = obj.parentNode.nextSibling.nextSibling.childNodes[0];
	total.innerText = (num * parseFloat(unit.innerText)).toFixed(2);
	totalMoney();
	changeCarNum(id,num);
}

function changeCarNum(id,num){
	var carData = JSON.parse(getCar());
	for (var i = 0; i < carData.length; i++) {
		if (carData[i].id == id) {
			carData[i].num = num;
			carData[i].totalPrice = (carData[i].num * carData[i].price).toFixed(2);
			break;
		}
	}
	addCar(carData);
}

function del(thisdel){
	var grandpar = thisdel.parentNode.parentNode.parentNode;
	var id = grandpar.getAttribute("id");
	delProduct(id);
	grandpar.remove();
	var index = 0;
	for (var j = 0;j<cbox.length;j++) {
		         if (cbox[j].checked==1){
		         	index++;
		         	if (index==cbox.length){
		         		checkAll[0].checked = 1;
						checkAll[1].checked = 1;
		         	}
		         }
			}
	if (cbox.length<1) {
		checkAll[0].checked = 0;
		checkAll[1].checked = 0;
	}
	choose_num.innerText = index;
	totalMoney();
}

function dels(thisdel){
	for(var i = 0; i<cbox.length;i++){
		if(cbox[i].checked){
			var grandpa = cbox[i].parentNode.parentNode;
			var id = grandpa.getAttribute("id");
			delProduct(id);
			grandpa.remove();
			i=-1;
		}
	}
	checkAll[0].checked = 0;
	checkAll[1].checked = 0;
	choose_num.innerText=0;
	price.innerText=0;
	sum.innerText=0;
	totalMoney();
}

function totalMoney(){
	var sum_price = 0;
	for(var i = 0; i<cbox.length;i++){
		cbox[i].index = i;
		if(cbox[i].checked){
			//console.log(total[cbox[i].index]);
			sum_price = sum_price + (parseFloat(total[cbox[i].index].innerText));			
		}	
	}
	sum.innerText=(sum_price).toFixed(2);
	price.innerText=(sum_price).toFixed(2);
}
